package logic

import (
	"context"
	"smart-health-platforms/config"
	"user_srv/internal/dao"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type ChatHistoryLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewChatHistoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatHistoryLogic {
	return &ChatHistoryLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ChatHistoryLogic) ChatHistory(in *user.ChatHistoryRequest) (*user.ChatHistoryResponse, error) {
	sgDao := dao.NewChatMessageDao(config.DB, l.svcCtx.Config)
	msgs, err := sgDao.List(l.ctx, in.SessionId)
	if err != nil {
		return nil, err
	}
	resp := &user.ChatHistoryResponse{}
	for _, m := range msgs {
		resp.Messages = append(resp.Messages, &user.Message{Role: m.Role, Content: m.Content})
	}
	return resp, nil

}
